package com.itheima.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.Emtity.ShoppingCart;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 添加到购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info(String.valueOf(shoppingCart));
//有bug:口味不一样,应该多家一条.  名称&口味&用户id相同 number+1

//        设置用户id,指定当前是哪个用户的购物车数据.
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

//         判断是否已经在购物车中.到底是菜品和套餐呢?需要判断一下.

        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);

        if (dishId != null) {
//    添加的是菜品
            lambdaQueryWrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else if (setmealId != null) {
//    添加的是套餐
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
//从数据库获得购物车数据,可能存在   可能为空.
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);
//        select * from  shoppingCart where  user_id   =? and dish / setmeal_id =?;
        if (one != null) {
//                该条记录已经存在,在数据库原有基础上+1
            Integer number = one.getNumber();
//        如果已经存在就number+1;
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
//            不在购物车中,这是一条新数据.
            //        如果不存在就存入表中.数量默认1;
            shoppingCart.setNumber(1);
//            设置事件
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }

    /**
     * 购物车列表数据
     *
     * @return
     */
    @GetMapping("list")
    public R<List<ShoppingCart>> list() {
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper();
//        根据用户id
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
//        当购物车东西多,作用就凸显出来,后加入的在底边.
//        根据降序
        lambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 清除购物车
     * @return
     */
    @DeleteMapping("clean")
    public R<String> clean() {
        // delete * from shopping_cart where user_id =?;
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId, currentId);
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("清除成功");

    }
}